﻿Public Class FormMain
    Dim objHWSetting As New clsHWSetting
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connectDB()
        setStatus()
        setData()
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_LHJ.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

    End Sub

    Public Sub setStatus()
        Dim dtStatus As New DataTable()
        dtStatus.Columns.Add("status_name")
        dtStatus.Columns.Add("status_value")
        dtStatus.Rows.Add("空闲", "0")
        dtStatus.Rows.Add("待出炉", "3")
        cmb_status.DisplayMember = "status_name"
        cmb_status.ValueMember = "status_value"
        cmb_status.DataSource = dtStatus
    End Sub

    Private Sub btnFresh_Click(sender As Object, e As EventArgs) Handles btnFresh.Click
        setData()
    End Sub

    Private Sub setData()
        Dim dt As DataTable = objHWSetting.selectAllLHJ()
        dt.Columns.Add("status_name")
        If dt.Rows.Count > 0 Then
            For Each row As DataRow In dt.Rows
                If row("status") = "0" Then
                    row("status_name") = "空闲"
                End If
                If row("status") = "1" Then
                    row("status_name") = "忙"
                End If
                If row("status") = "2" Then
                    row("status_name") = "未开机"
                End If
                If row("status") = "3" Then
                    row("status_name") = "待出炉"
                End If
                If row("status") = "4" Then
                    row("status_name") = "异常"
                End If
                If row("status") = "5" Then
                    row("status_name") = "维修中"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim lhj As String = dgv.SelectedRows(0).Cells("equip_id").Value.ToString()
            Dim rtn As Integer = objHWSetting.SetLHJstatus(cmb_status.SelectedValue.ToString(), "9", lhj)
            If rtn <> -2 Then
                setData()
                MsgBox("保存成功")
            Else
                MsgBox("保存失败")
            End If
        End If
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class